package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Commodity;
import cn.wolfcode.luowowo.article.domain.UserCommodity;
import cn.wolfcode.luowowo.article.mapper.CommodityMapper;
import cn.wolfcode.luowowo.article.mapper.UserCommodityMapper;
import cn.wolfcode.luowowo.article.service.ICommodityService;
import cn.wolfcode.luowowo.article.service.IUserCommodityService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserCommodityServiceImpl implements IUserCommodityService {

    @Autowired
    private UserCommodityMapper userCommodityMapper;

    @Reference
    private ICommodityService commodityService;

    @Override
    public void saveOrUpdate(UserCommodity userCommodity) {


            userCommodityMapper.insert(userCommodity);

    }

    @Override
    public void deleteByPrimaryKey(Long id) {

    }

    @Override
    public UserCommodity get(Long id) {
//        return userCommodityMapper.selectByPrimaryKey(id);
        return null;
    }

    @Override
    public List<UserCommodity> listAll() {
        return userCommodityMapper.selectAll();
    }

    @Override
    public List<UserCommodity> selectByUserId(Long uid) {
        return userCommodityMapper.selectByUserId(uid);
    }

    @Override
    public List<UserCommodity> selectParticularsByUserId(Long uid) {
        List<UserCommodity> ucs = userCommodityMapper.selectByUserId(uid);
        ArrayList<UserCommodity> list = new ArrayList<>();
        for (UserCommodity uc : ucs) {
            UserCommodity userCommodity = new UserCommodity();
            Commodity commodity = commodityService.get(uc.getCommodityId());
            try {
                BeanUtils.copyProperties(userCommodity,uc);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            userCommodity.setCommodity(commodity);
            list.add(userCommodity);
        }
        return list;
    }
}
